/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.core.util.GetFileList;
import cn.stylefeng.guns.modular.system.util.Field55Test;
import cn.stylefeng.guns.modular.system.util.TlvData;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import static cn.stylefeng.roses.core.util.HttpContext.getIp;

/**
 * 固件上传下载
 *
 * @author fengshuonan
 * @Date 2017年1月10日 下午8:25:24
 */
@Controller
@RequestMapping("/firmware")
public class FirmwareController extends BaseController {

    @RequestMapping(value = "/upload")
    public String uploadfile() {


        return "/system/firmware/uploadfile2.html";
    }

    @RequestMapping(value = "/download")
    public String downloadfile() {


        return "/system/firmware/downloadfile2.html";
    }

    /**
     * 实现文件上传
     * */
    @RequestMapping("/fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam("fileName") MultipartFile file){
        if(file.isEmpty()){
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        File dest = new File(  "/Users/xuyewen/Desktop/guntestfile/" + fileName);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            return "true";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/queryfiles", method = RequestMethod.POST)
    public String queryFiles() {
        System.out.print("queryfiles");
        HashMap<String,String> maps =GetFileList.getFileList("/Users/xuyewen/Desktop/guntestfile/");
        return JSON.toJSONString(maps);
    }


    @ResponseBody
    @RequestMapping(value = "/deviceinfo", method = RequestMethod.GET)
    public String deviceinfo(@RequestParam String info) {

        System.out.print("info==>"+info);
        List<TlvData> tlvs=Field55Test.parseFiled55Data(info);
        StringBuffer buffer=new StringBuffer();
        for (TlvData tlv:tlvs){
            buffer.append(tlv.getTag()+"<br/>");
        }
        return buffer.toString();
    }
}
